import collections
class Solution(object):
    def numTriplets(self, nums1, nums2):
        c1 = collections.Counter(nums1)
        c2 = collections.Counter(nums2)
        ans = 0
        for num in c1:
            for key in c2:
                if key < num:
                    if num ** 2 % key == 0 and num ** 2 // key in c2:
                        ans += c1[num] * c2[key] * c2[num ** 2 // key]
                if key == num:
                    ans += c1[num] * c2[key] * (c2[key] - 1) // 2
        for num in c2:
            for key in c1:
                if key < num:
                    if num ** 2 % key == 0 and num ** 2 // key in c1:
                        ans += c2[num] * c1[key] * c1[num ** 2 // key]
                if key == num:
                    ans += c2[num] * c1[key] * (c1[key] - 1) // 2
        return ans



data = Solution()
nums1 = [7,4]
nums2 = [5,2,8,9]
print(data.numTriplets(nums1, nums2))